package com.ia.aistream.manager.operating.util;

import com.alibaba.fastjson.JSONObject;
import com.ia.aistream.common.utils.spring.SpringUtils;
import com.ia.aistream.manager.framework.util.ShiroUtils;
import com.ia.aistream.manager.operating.util.style.DocStyleUtil;
import com.ia.aistream.mapper.operating.DocOperationSheetStyleMapper;
import com.ia.aistream.model.operating.entity.DocOperationSheetStyle;
import com.spire.doc.Table;
import com.spire.doc.TableCell;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 处理操作单数据
 * @author XiaoYu
 */
@Slf4j
public class DocOperationSheetUtil {

    /***
     * 实现操作单数据的处理
     * @param table
     * @param tid
     * @return
     * @throws UnsupportedEncodingException
     */
    public static Integer getOperationStyle(Table table, String tid) throws UnsupportedEncodingException {
        List<DocOperationSheetStyle> result = new ArrayList<>();
        int max = 0;
        int index=0;
        for (int r=0; r<table.getRows().getCount(); r++){
            DocOperationSheetStyle dos = new DocOperationSheetStyle();
            int count = table.getRows().get(r).getCells().getCount();
            dos.setRowNum((long) count);
            if(count==max){
                for(int c=0; c<count; c++) {
                    TableCell tableCell = table.getRows().get(r).getCells().get(c);
                    // 获取单元格的样式
                    JSONObject columnStyle = DocStyleUtil.getAppendixColumnStyle(table, r, c, max);
                    // 获取单元格内容
                    String cellContent = DocIndex.getCellContent(tableCell);
                    // 处理合并行的情况
                    String s = tableCell.getCellFormat().getVerticalMerge().toString();
                    if(s.equals("Continue")){
                        columnStyle = null;
                    }
                    switch(c){
                        case 0:
                            dos.setC1O(cellContent);
                            dos.setC1Style(columnStyle);
                            break;
                        case 1:
                            dos.setC2O(cellContent);
                            dos.setC2Style(columnStyle);
                            break;
                        case 2:
                            dos.setC3O(cellContent);
                            dos.setC3Style(columnStyle);
                            break;
                        case 3:
                            dos.setC4O(cellContent);
                            dos.setC4Style(columnStyle);
                            break;
                        case 4:
                            dos.setC5O(cellContent);
                            dos.setC5Style(columnStyle);
                            break;
                        default:
                            log.error("操作单异常，请联系管理员处理....");
                            break;
                    }
                }
            }else{
                max = count;
                r--;
                index++;
                continue;
            }

            dos.setCreateBy(ShiroUtils.getLoginName());
            dos.setCreateTime(new Date());
            dos.setDocTitleId(tid);
            dos.setRowType(index);

            result.add(dos);
        }

        DocOperationSheetStyleMapper dosMapper = SpringUtils.getBean("docOperationSheetStyleMapper");
        int i = dosMapper.insertDocOperationSheetStyleList(result);
        return i;
    }

    public static int getTableMax(Table table){
        int max = 0;
            for (int r=0; r<table.getRows().getCount(); r++){
                int count = table.getRows().get(r).getCells().getCount();
                if(count>max){
                   max = count;
                }
            }
        return max;
    }
}
